package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.admin.DishService;
import com.sky.service.admin.SetmealService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "菜品管理")
@Slf4j
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @PostMapping
    public Result save(@RequestBody DishDTO dto) throws CloneNotSupportedException {

        dishService.save(dto);
        return Result.success();

    }
    @GetMapping("/page")
    public Result<PageResult> pageFind(DishPageQueryDTO dto){
        PageResult pageResult= dishService.pageFind(dto);
        return Result.success(pageResult);
    }

    @DeleteMapping()
    public Result<String> delete(@RequestParam List<Integer> ids){
        dishService.delete(ids);
        return Result.success();
    }
    @GetMapping("/{dishId}")
    public Result<DishVO> selectById(@PathVariable Integer dishId){
       DishVO dishVO = dishService.selectById(dishId);
       return Result.success(dishVO);
    }

    @PutMapping()
    public Result<DishVO> put(@RequestBody DishDTO dto){
       DishVO vo= dishService.put(dto);
       return Result.success(vo);
    }
    @GetMapping("/list")
    public Result<List<DishVO>> findByCategoryId(Long categoryId){
        log.info("根据分类查询菜品id接收的参数:{}",categoryId);
        List<DishVO>list = setmealService.findByCategoryId(categoryId);
        return Result.success(list);
    }
}
